/**
 * @description 字典详情控制器
 * @author 韩
 */

const { generateSixDigitID } = require("../../../utils/crypto")
const dayjs = require("dayjs")
const { getFilter } = require("../../../utils/setFilter")
const dict_data_model = require("../../../model/system/dict/DICT.DATA.MODEL")

/**@description 添加修改控制器 */
const dictData_controller_save = async ({
    id = undefined,
    dictType = undefined,
    label = undefined,
    value = undefined,
    sort = undefined,
    colorType = undefined,
    cssClass = undefined,
    remark = undefined,
    status = undefined,
}) => {
    const params = {
        createDate: dayjs(),
        id: generateSixDigitID(),
        dictType,
        label,
        value,
        sort,
        colorType,
        cssClass,
        remark,
        status,

    }
    let addRes = null;
    if (!id) {
        //没有传id，去新建
        addRes = await dict_data_model.create({
            ...params
        })
    } else {
        // 有id 去修改
        addRes = await dict_data_model.findOneAndUpdate({ id: id }, {
            $set: {
                ...params
            }
        }, { new: true })
    }

    if (addRes) {
        const {
            createDate,
            id,
            dictType,
            label,
            value,
            sort,
            colorType,
            cssClass,
            remark,
            status,
        } = addRes

        return {
            code: 0,
            message: "成功",
            data: {
                createDate,
                id,
                dictType,
                label,
                value,
                sort,
                colorType,
                cssClass,
                remark,
                status,
            }
        }
    }
    return {
        status: "2000",
        message: "失败",
    }
}

/**@description 列表查询控制器 */
const dictData_controller_find = async ({ dictType = null, pageSize, current }) => {
    try {
        let filter = {
            // type: type && { $regex: eval("/" + type + "/i") },//名称模糊查询
            dictType: dictType && { $eq: dictType },//_id精确匹配
            // status: 1
        }
        filter = getFilter(filter)
        const list = await dict_data_model.find(filter).sort({ order: 1 }).skip((current - 1) * pageSize).limit(pageSize).lean();
        const totalCount = await dict_data_model.find(filter).countDocuments();
        return {
            code: 0,
            msg: "查询成功",
            data: {
                list,
                totalCount
            }
        }
    } catch (error) {
        return {
            code: 100,
            msg: "服务器故障"
        }
    }
}
/**@description 删除控制器 */
const dictData_controller_delete = async ({ ids = [] }) => {
    const deleteResult = await dict_data_model.deleteMany({
        id: { $in: ids }
    });


    if (deleteResult.deletedCount) {
        return {
            status: 0,
            message: `删除了${deleteResult.deletedCount}条数据`
        }
    } else {
        return {
            status: '0010',
            message: `删除失败`
        }
    }

}

/**@description 根据id查详情 */
const dictData_controller_get = async ({ id }) => {
    const res = await dict_data_model.findOne({
        id
    })
    if (res) {
        const {
            createDate,
            id,
            dictType,
            label,
            value,
            sort,
            colorType,
            cssClass,
            remark,
            status,
        } = res;

        return {
            code: 0,
            message: "查询成功",
            data: {
                createDate,
                id,
                dictType,
                label,
                value,
                sort,
                colorType,
                cssClass,
                remark,
                status,
            }
        }
    } else {
        return {
            status: 123,
            message: "未找到对应数据"
        }
    }
}


module.exports = {
    dictData_controller_save,
    dictData_controller_find,
    dictData_controller_delete,
    dictData_controller_get
}